﻿'Copyright (c) 2009 Grailtek

'Permission is hereby granted, free of charge, to any person
'obtaining a copy of this software and associated documentation
'files (the "Software"), to deal in the Software without
'restriction, including without limitation the rights to use,
'copy, modify, merge, publish, distribute, sublicense, and/or sell
'copies of the Software, and to permit persons to whom the
'Software is furnished to do so, subject to the following
'conditions:

'The above copyright notice and this permission notice shall be
'included in all copies or substantial portions of the Software.

'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'OTHER DEALINGS IN THE SOFTWARE.

Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Windows.Forms
Imports System.Text
Imports System.Aspect

Namespace Controls

    Public Class VerbsPanel

        Private _Assy As Assembly
        Public Property Assy() As Assembly
            Get
                Return _Assy
            End Get
            Set(ByVal value As Assembly)
                _Assy = value
                PopulateVerbs()
            End Set
        End Property

        Private Sub PopulateVerbs()
            Me.lbVerbs.Items.Clear()
            If Assy Is Nothing Then Return
            Dim asps As Verb() = System.ExecutorServices.GetVerbs(Assy, True)
            System.Array.Sort(asps)
            For Each asp As Verb In asps
                Dim i As Integer = Me.lbVerbs.Items.Add(asp)
                If Not asp.DelegateInfo.MethodInfo.IsPublic Then
                    'Me.ListBox1.r
                End If
            Next
        End Sub

        Private Sub lbVerbs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbVerbs.SelectedIndexChanged
            Dim d As DelegateInfo = Nothing
            Dim o As Object = Me.lbVerbs.SelectedItem
            If TypeOf o Is Verb Then d = DirectCast(o, Verb).DelegateInfo
            PopulateAspectsListBox(d)
            PopulateParams(d)
        End Sub

        Private Sub PopulateAspectsListBox(ByVal d As DelegateInfo)
            Me.lbVerbAspects.Items.Clear()
            If d Is Nothing Then Return
            lbVerbAspects.DisplayMember = "AspectUIName"
            'lbAspects.ValueMember = a
            For Each a As IAspect In d.Aspects
                Me.lbVerbAspects.Items.Add(a)
            Next
            If Me.lbVerbAspects.Items.Count > 0 Then
                Me.lbVerbAspects.SelectedIndex = 0
            Else
                Me.pgAspect.SelectedObject = Nothing
            End If
        End Sub

        Private Sub PopulateParams(ByVal d As DelegateInfo)
            If d Is Nothing Then
                pgParams.SelectedObjects = Nothing
            Else
                'pgParams.SelectedObject = d.Parameters
                pgParams.SelectedObject = d.GetEmptyArgs
            End If
        End Sub

        Public Class ParamInfo
            Public Sub New(ByVal p As ParameterInfo)
                _p = p
            End Sub
            Private _p As ParameterInfo
            Public Overrides Function ToString() As String
                Return _p.Name & " " & _p.ParameterType.Name
            End Function
        End Class

        Private Sub lbAspects_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbVerbAspects.SelectedIndexChanged
            Me.pgAspect.SelectedObject = Me.lbVerbAspects.SelectedItem
        End Sub

    End Class

End Namespace